package blog.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Notice;
import blog.service.NoticeService;

@Controller
@RequestMapping("/notice")
public class NoticeController {
	@Resource
	private NoticeService noticeService;

	/**
	 * 公告管理页
	 */
	@RequestMapping("")
	public String index(ModelMap m) {
		List<Notice> noticeList = noticeService.findAll();
		m.put("noticeList", noticeList);
		return "Notice/notice-list";
	}
	
	/**
	 * 删除公告
	 */
	@RequestMapping("/delete/{noticeleId}")
	public String delete(@PathVariable("noticeId") Integer noticeId)  {
		noticeService.deleteNoticeById(noticeId);
		return "forward:/notice";
	}
	
	/**
	 * 进入公告添加页面
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String gotoAddPage() {
		return "Notice/notice-add";
	}
	
	/**
	 * 进入公告添加页面
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Notice notice) {
		notice.setNoticeStatus(1);
		if(notice.getNoticeOrder()==null) {
			notice.setNoticeOrder(0);
		}
		notice.setNoticeCreateTime(LocalDateTime.now());
		notice.setNoticeUpdateTime(LocalDateTime.now());
		noticeService.add(notice);
		return "forward:/notice";
	}
}
